package element.synt;

import ast.AstItem;
import ast.ComplexDataSignature;
import ast.DataSignature;
import ast.PrimaryDataSignature;
import element.base.Synt;
import element.base.Word;

public class FuncFParam extends Synt {
    @Override
    public AstItem toAst() {
        PrimaryDataSignature pds = new PrimaryDataSignature();
        pds.type = DataSignature.DataType.PRIMARY;
        pds.dataType = ((Type) getNodes().getFirst()).findFirst().getContent();
        pds.name = ((Word) getNodes().get(1)).getContent();

        if (getNodes().size() == 2) {
            return pds;
        } else {
            int dimCount = (getNodes().size() - 2) / 2;
            ComplexDataSignature cds = new ComplexDataSignature();
            cds.type = DataSignature.DataType.COMPLEX;
            cds.dataOperated = pds;
            cds.opType = ComplexDataSignature.operationType.ARRAY_PARAM;

            PrimaryDataSignature defaultDim = new PrimaryDataSignature();
            defaultDim.type = DataSignature.DataType.PRIMARY;
            defaultDim.dataType = "int";
            defaultDim.value = -1;
            cds.operation.add(defaultDim);

            for (int i = 0; i < dimCount - 1; i++) {
                Exp dim = (Exp) getNodes().get(2 * i + 4);
                cds.operation.add(dim.toAst());
            }
            return cds;
        }
    }
}
